﻿
function loadingwait(file, bytesloaded) {
    //    var percent = Math.ceil((bytesloaded / file.size) * 100);
//    var percent;
//    if (file != undefined) {
//        //        alert(file.size);
//        percent = Math.ceil((bytesloaded / file.size) * 100);
//        try {
//            $('#bar' + (file.index+1)).width(percent);
//            $('#bar' + (file.index + 1)).html('<font style="color:white">' + percent + '%</font>');
//        }
//        catch (e) { }
    //    }
    if (file) {
        $('#bar').html('当前上传第' + (file.index + 1) + '个图片');
    }
    $('#bar').css({ 'background-image': 'url(img/progressbar_long.gif)' });
}
function loadinghide() {
    $('#bar').html('上传完毕！');
    $('#bar').css('background-image','');
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
    try {

        if (numFilesQueued > 0) {
            loadingwait();

            this.startUpload();
        }
    } catch (ex) {
        this.debug(ex);
    }
}
function uploadSuccess(file, serverData) {
    try {
        var obj = eval(serverData);
        if (obj.Success == true) {
            addImage(obj.Src,file);
        }
        else {
            var stats = swfu.getStats();
            stats.successful_uploads--;
            swfu.setStats(stats);
            alert(obj.Msg);
        }
        if (this.getStats().files_queued == 0) loadinghide();
    } catch (ex) {
        this.debug(ex);
    }
}
function uploadComplete(file) {
    try {

        if (this.getStats().files_queued > 0) {
            this.startUpload();
        }
    } catch (ex) {
        this.debug(ex);
    }
}

function addImage(src,file) {
    $('<img/>').attr({ 'src': src, "id": "img" + file.id }).css('margin-left', '5px').click(function() { window.open(src) }).appendTo($('#thumbimg'));
    $('<a/>').css({ 'font-size': '10px' }).html('Delete').click(function() { Del(this, "img" + file.id) }).appendTo($('#thumbimg'));
}

var swfu;
function mmswfuploadinit(sessionid, formauthid, count,filesize,filetype) {
    swfu = new SWFUpload({
    upload_url: "http://localhost:1294/upload.ashx",
        post_params: {
            "ASPSESSID": sessionid,
            "AUTHID": formauthid,
            "action":'upload'
        },
        file_size_limit: filesize || "10240000",
        file_types: filetype || "*.*",
        file_types_description: "pic",
        file_upload_limit: count,
        file_queue_limit: count,
        file_dialog_complete_handler: fileDialogComplete,
        upload_success_handler: uploadSuccess,
        upload_complete_handler: uploadComplete,
        upload_progress_handler: loadingwait,
        file_queue_error_handler: function() { alert('上传失败，请检查是否超出了文件上传的个数和大小限制。'); },
        flash_url: "js/swfupload.swf",
        debug: true,
        button_image_url: "img/AddPic.png", // Relative to the Flash file
        button_width: "85",
        button_height: "20",
        button_placeholder_id: "container",
        button_text: '<span>添加图片</span>',
        //				button_text_style: ".theFont { font-size: 18; }",
        button_text_left_padding: 15,
        button_text_top_padding: 0
    });
}

 function Del(obj,id) {
    if (!confirm("确定删除？")) {
        return;
    }
    var stats = swfu.getStats();
    stats.successful_uploads--;
    swfu.setStats(stats);
    $('#' + id).hide();
    obj.style.display = "none";
}

function Reset() {
    var stats = swfu.getStats();
    for (var i = 1; i <= 5; i++) {
        var url = $('#imgurl' + i).val(), urlold = $('#oldimgurl' + i).val();
        if (url == "" && urlold != "") {
            stats.successful_uploads++;
        }
        else if (url != "" && urlold == "") {
            stats.successful_uploads--;
        }
        $('#imgurl' + i).val(urlold);
        if ($('#imgurl' + i).val() == '') {
            $('#imga' + i)[0].href = 'javascript:void(0)';
            $('#imga' + i).hide();
            $('#imgspan' + i).hide();
        }
        else {
            $('#imga' + i)[0].href = urlold;
            $('#imga' + i).show();
            $('#imgspan' + i).show();
        }
        swfu.setStats(stats);
    }
    
}